class Solution:
    """
    5. 最长回文子串
    https://leetcode-cn.com/problems/longest-palindromic-substring/
    @date: 2021年1月2日16:00:36
    @author: mizzle rain
    """

    def longestPalindrome(self, s: str) -> str:
        m = len(s)
        if m <= 1:
            return s
        res = [0, 0]
        for i in range(m-1):
            a1, b1 = self._find(s, i, i)
            a2, b2 = self._find(s, i, i+1)
            res = [a1, b1] if b1-a1 > res[1]-res[0] else res
            res = [a2, b2] if b2-a2 > res[1]-res[0] else res
        return s[res[0]:res[1]]

    def _find(self, s, left, right):
        m = len(s)
        while left >= 0 and right < m and s[left] == s[right]:
            left -= 1
            right += 1
        return left+1, right
